#!/bin/bash

SRCDIR=`pwd`

cd org/eclipse/photran/internal/core/parser

echo Deleting old files...
rm -f *.java

echo Generating parser and AST classes...
java -Xmx1024M -cp $SRCDIR/ludwig.jar \
	Main_LALR1_Java_AST_Prototype \
	-startSymbols "<ExecutableProgram>, <Body>, <Expr>, <ContainsStmt>" \
	-package "org.eclipse.photran.internal.core.parser" \
	-shift T_COMMA "<IoControlSpecList>" \
	-reduce "<SFVarName>" "<SFDummyArgName>" \
	-prec "<DeferredShapeSpecList> > <AssumedShapeSpec>" \
	-prec "<BodyPlusInternals> > <Label>, T_ICON" \
	-prec "<TypeBoundProcedurePart> > T_ICON" \
	-shift T_IDENT "<CommonBlock>" \
	-shift T_LBRACKET "<DataRef>" \
	-shift T_LBRACKET "<SFDataRef>" \
	-imports "import org.eclipse.photran.internal.core.lexer.*; \
                  import org.eclipse.photran.internal.core.analysis.binding.ScopingNode; \
                  import org.eclipse.photran.internal.core.SyntaxException; \
                  import java.io.IOException;" \
	-description "An LALR(1) parser for Fortran 2008" \
	-throws "IOException, LexerException, SyntaxException" \
	-onError "throw new SyntaxException(parserStack.getLookahead(), describeTerminalsExpectedInCurrentState());" \
	-lexerClass IAccumulatingLexer \
	-parserClass Parser \
	-tokenType "org.eclipse.photran.internal.core.lexer.Token" \
	-autolabel true \
	-genTerminalClass false \
	-genLexerInterface false \
	-genTokenClass false \
	-separateSemanticActions true \
	-separateParsingTables true \
	-enablePreproc IPreprocessorReplacement \
	$SRCDIR/fortran2008.bnf

echo Applying patches...
for file in *.java; do
  patch $file <$SRCDIR/Copyright.patch >/dev/null
done
patch ASTSubroutineArgNode.java <$SRCDIR/ASTSubroutineArgNode.patch

echo "Done"
find . -name "*.rej"
